/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.kaede.sample.slice;

import me.kaede.sample.ResourceTable;
import me.kaede.sample.TestData;
import me.kaede.tagview.OnTagClickListener;
import me.kaede.tagview.OnTagDeleteListener;
import me.kaede.tagview.Tag;
import me.kaede.tagview.TagView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

import java.security.SecureRandom;

/**
 * MainAbilitySlice
 *
 * @since 2021-03-24
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final float MLAYOUTBORDERSIZE3F = 3f;
    private static final int MARRAY = 3;
    private static final float MRADIUS20 = 20f;
    private static final float MRADIUS60 = 60f;
    private static final float MTAGTEXTSIZE14 = 14f;
    private static final int MNEXTINT = 5;
    private static final float MLAYOUTBORDERSIZE = 2f;
    private static final String BORDER = "Border";
    private static final String ROUNDCORNER = "Round Corner";
    private static final int NEXTINT2 = 2;
    private TagView tagView;
    private Text mTvAdd;
    private TextField editTag;
    private SecureRandom random;
    private Text tvListAbility;
    private Text tvRecyclerviewAbility;

    /**
     * onStart
     *
     * @param intent
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        try {
            getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
        } catch (IllegalArgumentException e) {
            e.getMessage();
        }
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
        random = new SecureRandom();
        initClickListener();
        tagView.addTags(TestData.getContinents());
        String[] colors = TestData.getColorsList();
        for (int index = 1; index < colors.length; index++) {
            Tag tag = new Tag("Colorful Text");
            tag.tagTextColor = Color.getIntColor(colors[index]);
            tagView.addTag(tag);
        }

        for (String item : colors) {
            Tag tag = new Tag("Colorful Background");
            tag.layoutColor = Color.getIntColor(item);
            tagView.addTag(tag);
        }

        Tag tag = new Tag(BORDER);
        tag.layoutBorderSize = 1f;
        tagView.addTag(tag);

        tag = new Tag(BORDER);
        tag.layoutBorderSize = MLAYOUTBORDERSIZE;
        tag.layoutBorderColor = Color.getIntColor(colors[1]);
        tagView.addTag(tag);

        tag = new Tag(BORDER);
        tag.layoutBorderSize = MLAYOUTBORDERSIZE3F;
        tag.layoutBorderColor = Color.getIntColor(colors[MARRAY]);
        tagView.addTag(tag);

        tag = new Tag(ROUNDCORNER);
        tag.radius = 0f;
        tagView.addTag(tag);

        tag = new Tag(ROUNDCORNER);
        tag.radius = MRADIUS20;
        tagView.addTag(tag);

        tag = new Tag(ROUNDCORNER);
        tag.radius = MRADIUS60;
        tagView.addTag(tag);

        tag = new Tag("Deletable");
        tag.isDeletable = true;
        tagView.addTag(tag);

        addCustomizeTagView();
        setClickInit();
    }

    /**
     * initView
     */
    private void initView() {
        tagView = (TagView) findComponentById(ResourceTable.Id_tagview);
        mTvAdd = (Text) findComponentById(ResourceTable.Id_tv_add);
        editTag = (TextField) findComponentById(ResourceTable.Id_edit_tag);
        tvListAbility = (Text) findComponentById(ResourceTable.Id_tv_list_ability);
        tvRecyclerviewAbility = (Text) findComponentById(ResourceTable.Id_tv_recyclerview_ability);
    }

    /**
     * initClickListener
     */
    private void initClickListener() {
        tagView.setOnTagClickListener(new OnTagClickListener() {
            @Override
            public void onTagClick(int position, Tag tag) {
                new ToastDialog(MainAbilitySlice.this)
                        .setText("click tag id = " + tag.id + " position = " + position)
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });
        tagView.setOnTagDeleteListener(new OnTagDeleteListener() {
            @Override
            public void onTagDeleted(int position, Tag tag) {
                new ToastDialog(MainAbilitySlice.this)
                        .setText("delete tag id = " + tag.id + " position = " + position)
                        .setAlignment(LayoutAlignment.CENTER)
                        .show();
            }
        });
    }

    /**
     * setClickInit
     */
    private void setClickInit() {
        mTvAdd.setClickedListener(view -> {
            String string = "ADD A TAG";
            if (editTag.getText() != null && !editTag.getText().equals("")) {
                string = editTag.getText();
            }
            Tag tag = new Tag(string);
            int ran = random.nextInt(NEXTINT2);
            if (ran == 0) {
                tag.isDeletable = true;
            }
            ran = random.nextInt(MNEXTINT);
            tag.layoutColor = Color.getIntColor(TestData.getColorsList()[ran]);
            tagView.addTag(tag);
        });

        tvListAbility.setClickedListener(view -> {
            present(new ListViewAbilitySlice(), new Intent());
        });

        tvRecyclerviewAbility.setClickedListener(view -> {
            present(new RecyclerViewAbilitySlice(), new Intent());
        });
    }

    /**
     * addCustomizeTagView
     */
    private void addCustomizeTagView() {
        Tag tag = new Tag("Detail Tag");
        tag.tagTextColor = Color.getIntColor("#FFFFFF");
        tag.layoutColor = Color.getIntColor("#DDDDDD");
        tag.layoutColorPress = Color.getIntColor("#555555");
        tag.radius = MRADIUS20;
        tag.tagTextSize = MTAGTEXTSIZE14;
        tag.layoutBorderSize = 1f;
        tag.layoutBorderColor = Color.getIntColor("#FFFFFF");
        tag.isDeletable = true;
        tagView.addTag(tag);
    }

    /**
     * onActive
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     * onForeground
     *
     * @param intent
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
